<?php

namespace app\common\model\service\order;

use app\common\model\Service;
use app\common\model\service\Category;
use app\common\model\service\Order;
use think\Model;


class Info extends Model
{





    // 表名
    protected $name = 'service_order_data';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'paytime_text'
    ];






    public function getTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['time']) ? $data['time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getPaytimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paytime']) ? $data['paytime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setPaytimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    public static function addData($order,$time)
    {
        $data = [
            'order_id' => $order->id,
            'sn' => sn('service_order_data', 'sn', 'DO'),
            'renewal_amount' => $order->pay_amount,
            'time' => $time,
        ];
        $model = new self;
        $model->allowField(true)->save($data);
        return $model;
    }
    public static function orderRenewal($data)
    {
        $order = Order::get($data['id']);
        $service = Service::get($order->cate_id);
        $params['pay_amount'] = $service->renewal;
//        if ($service->category_ids) {
//            $service_category = Category::get($order->category_id);
//            $params['pay_amount'] = bcmul($service_category->renewal, $data['time'], 2);
//        }

        $data = [
            'order_id' => $data['id'],
            'sn' => sn('service_order_data', 'sn', 'DO'),
            'renewal_amount' =>  $params['pay_amount'],
            'time' => $data['time'],
            'status' => 1
        ];
        $model = new self;
        $model->allowField(true)->save($data);
        return $model;
    }
}
